#!/bin/bash
# 初始化centos服务器

#必须是root用户才可以执行
if [ $UID -ne 0 ]; then
    echo "Superuser privileges are required to run this script."
    echo "e.g. \"sudo $0\""
    exit 1
fi

#设置一下主机名
echo -n "please set the host name："
read hostname

if [[ ! $hostname ]]; then
  echo "$0 hostname is required"
  exit 1
fi

echo "setting host name to $hostname"
hostnamectl --static --transient --pretty set-hostname $hostname

hostnamectl --static --transient --pretty set-hostname lecai-node5-96

echo -n "please set the domain："
read domain
echo "export domain=${domain}" >> /etc/environment

echo -n "is dev mode?(y/n): "
read devmode

#echo -n "need mysql?(y/n):"
#read needMysql

# 资源目录，总是在deployment的根目录
resourcebasepath=$(cd `dirname $0`; cd ../resource; pwd) #/root/resource

# 安装一些必要的软件并启动相关服务
yum -y install net-tools ntp redis memcached httpd lvm2 firewalld wget device-mapper

systemctl enable ntpd
systemctl enable redis
systemctl enable memcached
systemctl enable httpd
systemctl enable firewalld
systemctl start firewalld

# 将http端口开放
firewall-cmd --add-service=http --permanent

# 安装tomcat，清华大学源，据说速度很快
echo "starting to install tomcat8"
cd $resourcebasepath
cd tomcat

tar -C /usr -xzf apache-tomcat-8.0.47.tar.gz

catalinaHome=`ls /usr/apache-tomcat* -d`

# tomcat空实例
cp tomcat_base.tar.gz ${catalinaHome}/
#  systemctl管理需要用到的tomcat.service
cp tomcat.service ${catalinaHome}/bin/

chown -R root:root ${catalinaHome}
chmod +x ${catalinaHome}/bin/*.sh

# jdbc
cd $resourcebasepath
cd jdbc
cp *.jar ${catalinaHome}/lib

# 设置环境变量
echo "export CATALINA_HOME=${catalinaHome}" >> /etc/environment

# 安装jdk
echo "starting to install jdk1.8"
cd $resourcebasepath
cd java
rpm -ivh jdk-8u151-linux-x64.rpm
# 设置环境变量
echo "export JAVA_HOME=/usr/java/jdk1.8.0_151" >> /etc/environment
echo "export JRE_HOME=/usr/java/jdk1.7.0_79/jre" >> /etc/environment

# httpd

if [[ ! -e /data1 ]]; then
    mkdir /data1
    if [[ ! -e /data1/cdn ]]; then
        mkdir /data1/cdn
    fi
fi

echo "starting to config httpd"

cd $resourcebasepath
cd httpd
cp *.so /etc/httpd/modules/
cp *.conf /etc/httpd/conf.modules.d/
cp *.properties /etc/httpd/conf/
service httpd configtest
cp httpd-resource.conf /etc/httpd/conf.d/0-resource.conf
sed -i -e "s@#{domain}@"${domain}"@g"\
 /etc/httpd/conf.d/0-resource.conf

chmod +r /etc/httpd/conf.d/0-resource.conf
apachectl configtest

# 是否是dev模式
if [[ ${devmode} = 'y' ]]; then
    # 安装jenkins，需要设置端口，8088
    echo "starting to install jenkins"
    wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
    rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    yum -y install jenkins
    # 开放8088端口
    firewall-cmd --zone=public --add-port=8088/tcp --permanent

    # 启动jenkins
#    service jenkins start

    # 安装禅道
    echo "starting to install zentao"
    cd $resourcebasepath
    cd zentao
    tar -zxvf ZenTaoPMS.9.6.1.zbox_64.tar.gz -C /opt
    cd /opt/zbox/
    # 设置禅道端口
    ./zbox -ap 8089
    # 启动禅道
    ./zbox start
    # 开放8089端口
    firewall-cmd --zone=public --add-port=8089/tcp --permanent
    firewall-cmd --reload

    # 创建一个用来分享资源的用户
    useradd res
    echo "res123456" |passwd --stdin allan
    cd /data1/cdn
    mkdir dev
    chown res dev/
    chown :res dev/
    usermod -aG res apache
fi

# 使得环境变量生效
source /etc/environment

# 添加自己并加入root权限
#useradd allan
#usermod -aG wheel allan
#echo "efs123456" |passwd --stdin allan
#chage -d0 allan

echo "init server successfully!!!"


